package com.geostar.constructionland.analyse.service.handler.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.geostar.constructionland.analyse.entity.HttpResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResultOverlapLand;
import com.geostar.constructionland.analyse.entity.model.AnalyseModel;
import com.geostar.constructionland.analyse.entity.resulthandler.HandlerResult;
import com.geostar.constructionland.analyse.mapper.sptalyresult.AlyResultOverlapLandMapper;
import com.geostar.constructionland.analyse.service.analyseresult.OvpLandService;
import com.geostar.constructionland.analyse.service.handler.IResultHandler;
import com.geostar.constructionland.analyse.service.handler.impl.util.XmlUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

/**
 * @author yelll
 * @since 2020-6-8 16:50
 **/
@Component
@Slf4j
public class OverlapLandHandler implements IResultHandler {
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    AlyResultOverlapLandMapper alyResultOverlapLandMapper;
    @Autowired
    OvpLandService ovpLandService;
    @Override
    public HandlerResult handler(AnalyseModel analyseModel, HttpResult httpResult) throws Exception {
        log.info("处理重叠宗地分析返回数据,状态码:{}",httpResult.getCode());
        HandlerResult handlerResult = HandlerResult.createBySuccessMessage("操作无误，恭喜成功!");
        if(HttpStatus.SC_OK==httpResult.getCode()){
            /**解析xml格式,获取JSON数据**/
            String newBody = XmlUtil.parseXml(httpResult.getBody());
            httpResult.setBody(newBody);
            /**保存占压信息**/
            AlyResult alyResult = objectMapper.readValue(httpResult.getBody(),AlyResult.class);
            log.info("body:"+httpResult.getBody());
            if(HttpStatus.SC_OK == alyResult.getStatus()){
                /**如果成功,构建主体解析**/
                List<AlyResultOverlapLand> alyResultOverlapLandList = objectMapper.readValue(alyResult.getAnalysisResult().getResultRecords(),
                        new TypeReference<List<AlyResultOverlapLand>>(){});
                if(alyResultOverlapLandList.size()>0){
                    /**设置分析ID**/
                    alyResultOverlapLandList.forEach(alyResultOverlapLand -> alyResultOverlapLand.setAlyCode(analyseModel.getAlyId()));
                    /**批量插入数据**/
                    ovpLandService.saveBatch(alyResultOverlapLandList);
                    handlerResult = HandlerResult.createByHave_overlap_land_error("有宗地占压哦",httpResult.getBody());
                }
            }else{
                throw new Exception(alyResult.getMessage());
            }
        }else {
            handlerResult = HandlerResult.createBySpatiaAnalyse_error("调用分析失败，请查看分析结果内容",httpResult);
        }
        return handlerResult;
    }
}
